package com.learn;

import java.awt.*;
import java.awt.event.*;
import java.rmi.dgc.DGC;
import java.sql.SQLException;

import javax.swing.*;

public class InputWindow extends JPanel implements ActionListener{
	JLabel[] labels;
	String[] labelNames={"学号：","姓名：","班级：","专业：","社团：","职位："};
	JTextField numberField,nameField,clazzField,majorField,commuField,positionField;
	
	JButton inputButton,resetButton;
	DBConnection dbc;
	
	public void init(){
		Font labelFont=new Font("宋体",Font.PLAIN,20); //标签字体
		labels=new JLabel[6];
		for(int i=0;i<6;i++){
			labels[i]=new JLabel(labelNames[i],JLabel.CENTER);
			labels[i].setFont(labelFont);
		}
		
		numberField=new JTextField(20);
		numberField.setFont(labelFont);
		nameField=new JTextField(20);
		nameField.setFont(labelFont);
		clazzField=new JTextField(20);
		clazzField.setFont(labelFont);
		majorField=new JTextField(20);
		majorField.setFont(labelFont);
		commuField=new JTextField(20);
		commuField.setFont(labelFont);
		positionField=new JTextField(20);
		positionField.setFont(labelFont);
		
		inputButton=new JButton("录入");
		resetButton=new JButton("重置");
		
		Box[] horiBoxes=new Box[6];
		for(int i=0;i<6;i++){
			horiBoxes[i]=Box.createHorizontalBox();
			horiBoxes[i].add(labels[i]);
		}
		horiBoxes[0].add(numberField);
		horiBoxes[1].add(nameField);
		horiBoxes[2].add(clazzField);
		horiBoxes[3].add(majorField);
		horiBoxes[4].add(commuField);
		horiBoxes[5].add(positionField);
		
		Box verticalBox=Box.createVerticalBox();
		for(int i=0;i<6;i++){
			verticalBox.add(horiBoxes[i]);
			verticalBox.add(Box.createVerticalStrut(15));
		}
		
		JPanel centerPanel=new JPanel();
		centerPanel.setOpaque(false);
		centerPanel.add(verticalBox);
		JPanel buttonPanel =new JPanel();
		buttonPanel.add(inputButton);
		buttonPanel.add(resetButton);
		inputButton.addActionListener(this);
		resetButton.addActionListener(this);
		
		//设置边界式布局
		this.setLayout(new BorderLayout());
		this.add(centerPanel,BorderLayout.CENTER);
		this.add(buttonPanel,BorderLayout.SOUTH);
		dbc=new DBConnection();
	}
	public InputWindow(){
		init();
	}
	
	
	public boolean isLegal(String str){//判断文本框内的内容是否非空
		if(str==null || str.equals("") || str.contains(" "))
			return false;
		return true;
	}
	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getActionCommand().equals("重置")){//点击了重置按钮
			numberField.setText("");
			nameField.setText("");
			clazzField.setText("");
			majorField.setText("");
			commuField.setText("");
			positionField.setText("");
		}else {//点击了录入按钮
			if(!isLegal(numberField.getText())){//学号为空或包含空格
				JOptionPane.showMessageDialog(this, "您输入的学号不规范：学号为空或包含空格！",
						"学号非法",JOptionPane.OK_OPTION);
				numberField.requestFocus();
				numberField.selectAll();
			}else if(!isLegal(nameField.getText())){//姓名为空或包含空格
				JOptionPane.showMessageDialog(this, "您输入的姓名不规范：姓名为空或包含空格！",
						"姓名非法",JOptionPane.OK_OPTION);
				nameField.requestFocus();
				nameField.selectAll();
			}else if(!isLegal(clazzField.getText())){//班级为空或包含空格
				JOptionPane.showMessageDialog(this, "您输入的班级不规范：班级为空或包含空格！",
						"班级非法",JOptionPane.OK_OPTION);
				clazzField.requestFocus();
				clazzField.selectAll();
			}else{
				try {
					if(dbc==null||dbc.getCon()==null||dbc.getCon().isClosed()){
						dbc=new DBConnection();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
				if(dbc.findByNumber(numberField.getText())){
					JOptionPane.showMessageDialog(this, "您输入的学号已存在，请重新输入！",
							"学号重复",JOptionPane.OK_OPTION);
					numberField.requestFocus();
					numberField.selectAll();
				}else{//将新输入的学生信息插入数据表student
					
					String [] strs={numberField.getText(),nameField.getText(),clazzField.getText(),majorField.getText(),commuField.getText(),positionField.getText()};	
					if(dbc.insertRecord(strs)){
						JOptionPane.showMessageDialog(this, "您插入学生信息成功！",
								"插入提示",JOptionPane.PLAIN_MESSAGE);
					}else{
						JOptionPane.showMessageDialog(this, "您插入学生信息失败！",
								"插入提示",JOptionPane.OK_OPTION);
					}
				}
				dbc.close();
			}
		}
		
	}
}
